package api

import (
	"TaskAssignmentSystem/internal/admin/dao"
	"TaskAssignmentSystem/internal/admin/services"
	adminServices "TaskAssignmentSystem/internal/admin/services"
	"TaskAssignmentSystem/internal/pkg/middleware/handler"
	"TaskAssignmentSystem/pkg/utils"
	"errors"
	"github.com/kataras/iris/v12"
	"log"
	"os/exec"
)

type TaxRatioRequest struct {
	TotalAmount float64 `json:"total_amount"`
}

func TaxRatioHandler(ctx iris.Context) {
	var serve services.SystemDictService
	if taxRatio, des, err := serve.GetTaxRatio(); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseData(iris.Map{"taxRatio": taxRatio, "des": des}))
		return
	}
}

func CalculationTaxAmountHandler(ctx iris.Context) {
	var RequestParams TaxRatioRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}
	//判断权限
	var claims adminServices.CustomClaims
	if err := claims.ParseClaims(ctx); err != nil {
		ctx.JSON(utils.ResponseError{Code: iris.StatusInternalServerError, Message: err.Error()})
		return
	}
	var userServers services.UserService
	//校验用户是否存在
	exist, user, err := userServers.VerifyAndQueryUser(claims.Id)
	if err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	}
	if !exist {
		ctx.JSON(utils.ResponseServerError(errors.New("该用户不存在")))
		return
	}
	var serve services.SystemDictService
	if taxAmount, err := serve.GetTaxAmount(user.UserId, RequestParams.TotalAmount); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseData(iris.Map{"taxAmount": taxAmount}))
		return
	}
}

type QuerySystemDictRequest struct {
	DictCode string `json:"dict_code" validate:"required"`
}

func QuerySystemDictHandler(ctx iris.Context) {
	var RequestParams QuerySystemDictRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}

	var dictServe services.SystemDictService
	if res, err := dictServe.QuerySystemDict(RequestParams.DictCode); err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseData(res))
		return
	}
}

type UpdateSystemDictRequest struct {
	DictCode    string `json:"dict_code" validate:"required"`
	Description string `json:"description"`
	DictValue   string `json:"dict_value"`
}

func UpdateSystemDictHandler(ctx iris.Context) {
	var RequestParams UpdateSystemDictRequest
	if err := ctx.ReadJSON(&RequestParams); err != nil {
		handler.ArgumentErrorHandle(err, ctx)
		return
	}

	var dictDao dao.Common[dao.SystemDict]
	updateMap := map[string]interface{}{}
	updateMap["dict_code"] = RequestParams.DictCode
	if utils.StringIsNotEmpty(RequestParams.Description) {
		updateMap["description"] = RequestParams.Description
	}
	if utils.StringIsNotEmpty(RequestParams.DictValue) {
		updateMap["dict_value"] = RequestParams.DictValue
	}
	if err := dictDao.BatchUpdateWithWhere(map[string]interface{}{"dict_code": RequestParams.DictCode}, updateMap).Error; err != nil {
		ctx.JSON(utils.ResponseServerError(err))
		return
	} else {
		ctx.JSON(utils.ResponseOK())
		return
	}
}

func ClearServerCacheHandler(ctx iris.Context) {
	// 指定要执行的Shell脚本文件
	scriptPath := "/docker/go/empty_files.sh"

	// 创建一个exec.Cmd结构体来表示要执行的命令
	cmd := exec.Command("/bin/bash", scriptPath)

	// 执行命令并捕获标准输出和标准错误
	output, err := cmd.CombinedOutput()

	// 检查是否有错误发生
	if err != nil {
		log.Fatalf("Error executing script: %v\nOutput:\n%s", err, output)
	}

	// 打印脚本的输出
	log.Printf("Script executed successfully. Output:\n%s", output)

	ctx.JSON(utils.ResponseOK())
	return
}
